unit capturaaliquotas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Grids;

type
  TFormPrincipal = class(TForm)
    Button1: TButton;
    StatusBar1: TStatusBar;
    SG_Aliquotas: TStringGrid;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  type
        RegDecompoeAliquota = record
        Tipo: Char;
        Indice: Char;
        Taxa: Array[0..3] of char ;
        Separador: Char ;
    end;
  type
        RegDecompoeTParcial = record
        Tributados: Array[0..15] of Array[0..13] of char;
        I: Array[0..13] of char;
        N: Array[0..13] of char;
        F: Array[0..13] of char;
        NaoFiscais: Array[0..8] of Array[0..13] of char;
        Sangria: Array[0..13] of char;
        Suprimento: Array[0..13] of char;
        GT: Array[0..17] of char;

    end;


var
  FormPrincipal: TFormPrincipal;
  Resposta: integer;
  Conteudo:Array[0..3020] of char; // Conteudo de retorno das funções de status
  cTributacoes:Array[1..20] of RegdecompoeAliquota;
  tTributacoes:RegdecompoeTParcial;

  Procedure WaitProcess;
  procedure HabilitaDesabilitaItensMenu(Situacao:Integer);

  function ECF_AbrePortaSerial: Integer; StdCall; External 'CONVECF.dll';
  function ECF_FechaPortaSerial: Integer; StdCall; External 'CONVECF.dll';
  
  function ECF_LerAliquotasComIndice(Tribut:Pchar ):integer;StdCall; External 'CONVECF.dll';
  function ECF_VerificaTotalizadoresParciais(Texto:Pchar ):integer;StdCall; External 'CONVECF.dll';


implementation

{$R *.dfm}

procedure TFormPrincipal.Button1Click(Sender: TObject);
var i:integer;
begin


        // ALÍQUOTAS -----------------------------------------------------------------
//        cAliquotas:=#0;
        Statusbar1.Panels[0].Text:='Ler Alíquotas com Índice'; WaitProcess;

        Statusbar1.Panels[0].Text:='Capturando Alíquotas'; WaitProcess;
        Resposta:=ECF_LerAliquotasComIndice(Conteudo);
        CopyMemory(@cTributacoes,@Conteudo,300);
        if Resposta = 1 then begin
                SG_Aliquotas.Cells[0,0]:='Tipo';
                SG_Aliquotas.Cells[1,0]:='Índice';
                SG_Aliquotas.Cells[2,0]:='Taxa';
                for i:=1 to 50 do
                begin
                       SG_Aliquotas.Cells[0,i]:=cTributacoes[i].tipo;
                       SG_Aliquotas.Cells[1,i]:=cTributacoes[i].indice;
                       SG_Aliquotas.Cells[2,i]:=cTributacoes[i].taxa;
                end;
        end;
        StatusBar1.Panels[1].Text:='Ler Alíquotas com Índice:'+inttostr(Resposta);

        Resposta:=ECF_VerificaTotalizadoresParciais(Conteudo);
        CopyMemory(@tTributacoes,@Conteudo,445);
        if Resposta = 1 then begin
                SG_Aliquotas.Cells[3,0]:='Total';
                for i:=0 to 50 do
                begin
                       SG_Aliquotas.Cells[3,i+1]:=tTributacoes.tributados[i];
                end;
        end;

        StatusBar1.Panels[1].Text:='Verifica Totalizadores Parciais:'+inttostr(Resposta);
        HabilitaDesabilitaItensMenu(1); // Habilita menu


end;

Procedure WaitProcess;
begin
        HabilitaDesabilitaItensMenu(0); // Desabilita menu
        FormPrincipal.Statusbar1.Panels[1].Text:='Aguarde ...';
        FormPrincipal.Cursor:=crHourGlass;
        FormPrincipal.Refresh;
end;
procedure HabilitaDesabilitaItensMenu(Situacao:Integer);
// 0 =desabilita e 1 = habilita
begin
    if situacao= 0 then FormPrincipal.Enabled:=False
    else  begin
        FormPrincipal.Enabled:=True;
        FormPrincipal.Cursor:=crDefault;
    end;


end;


procedure TFormPrincipal.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
        ECF_FechaPortaSerial;
end;

procedure TFormPrincipal.Button2Click(Sender: TObject);
begin
        Statusbar1.Panels[0].Text:='Abre Porta Serial'; WaitProcess;
        Resposta:=ECF_AbrePortaSerial();
        Statusbar1.Panels[0].Text:=' '; WaitProcess;
        StatusBar1.Panels[1].Text:='Abre Porta Serial:'+inttostr(Resposta);
        HabilitaDesabilitaItensMenu(1); // Habilita menu
end;

procedure TFormPrincipal.Button3Click(Sender: TObject);
begin
        Statusbar1.Panels[0].Text:='Fecha Porta Serial'; WaitProcess;
        Resposta:=ECF_FechaPortaSerial();
        Statusbar1.Panels[0].Text:=' '; WaitProcess;
        StatusBar1.Panels[1].Text:='Fecha Porta Serial:'+inttostr(Resposta);
        HabilitaDesabilitaItensMenu(1); // Habilita menu

end;

end.
